<template>
  <div class="login">
    <div class="login-container">
      <div class="login-header">
        <h1 class="logo">{{ title }}</h1>
        <p class="subtitle">欢迎使用企业管理系统</p>
      </div>

      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            type="text"
            auto-complete="off"
            placeholder="请输入账号"
            size="large"
          >
            <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="请输入密码"
            size="large"
            @keyup.enter.native="handleLogin"
          >
            <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>

        <el-form-item prop="code" v-if="captchaEnabled" class="captcha-item">
          <div class="captcha-container">
            <el-input
              v-model="loginForm.code"
              auto-complete="off"
              placeholder="请输入验证码"
              size="large"
              @keyup.enter.native="handleLogin"
            >
              <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
            </el-input>
            <div class="login-code">
              <img :src="codeUrl" @click="getCode" class="login-code-img"/>
            </div>
          </div>
        </el-form-item>

        <div class="form-options">
          <el-checkbox v-model="loginForm.rememberMe" class="remember-me">记住密码</el-checkbox>
          <div v-if="register" class="register-link">
            <router-link class="link-type" :to="'/register'">立即注册</router-link>
          </div>
        </div>

        <el-form-item class="login-button-item">
          <el-button
            :loading="loading"
            size="large"
            type="primary"
            class="login-button"
            @click.native.prevent="handleLogin"
          >
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2025 YourCompany All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login"
import Cookies from "js-cookie"
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      title: process.env.VUE_APP_TITLE,
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCode()
    this.getCookie()
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img
          this.loginForm.uuid = res.uuid
        }
      })
    },
    getCookie() {
      const username = Cookies.get("username")
      const password = Cookies.get("password")
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 })
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 })
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 })
          } else {
            Cookies.remove("username")
            Cookies.remove("password")
            Cookies.remove('rememberMe')
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{})
          }).catch(() => {
            this.loading = false
            if (this.captchaEnabled) {
              this.getCode()
            }
          })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #2c3e50 0%, #34495e 50%, #2c3e50 100%);
  position: relative;
  overflow: hidden;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: url("../assets/images/9f0e84e94fc94492afda1793afaf31fb.jpg");
    background-size: cover;
    background-position: center;
    opacity: 0.1;
    z-index: 1;
  }

  &::after {
    content: '';
    position: absolute;
    top: -50%;
    left: -50%;
    width: 200%;
    height: 200%;
    background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
    animation: float 20s ease-in-out infinite;
    z-index: 1;
  }
}

@keyframes float {
  0%, 100% {
    transform: translate(0, 0) rotate(0deg);
  }
  25% {
    transform: translate(10px, 10px) rotate(1deg);
  }
  50% {
    transform: translate(-5px, 20px) rotate(-1deg);
  }
  75% {
    transform: translate(-10px, -5px) rotate(1deg);
  }
}

.login-container {
  position: relative;
  z-index: 2;
  width: 100%;
  max-width: 420px;
  margin: 0 20px;
  animation: slideInUp 0.8s ease-out;
}

@keyframes slideInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.login-header {
  text-align: center;
  margin-bottom: 40px;

  .logo {
    font-size: 32px;
    font-weight: 600;
    color: #ffffff;
    margin: 0 0 10px 0;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
    letter-spacing: 1px;
    animation: glow 2s ease-in-out infinite alternate;
  }

  .subtitle {
    font-size: 16px;
    color: #bdc3c7;
    margin: 0;
    font-weight: 300;
    animation: fadeIn 1s ease-out 0.5s both;
  }
}

@keyframes glow {
  from {
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
  }
  to {
    text-shadow: 0 2px 20px rgba(255, 255, 255, 0.5);
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.login-form {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  padding: 40px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  transition: all 0.3s ease;
  position: relative;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    border-radius: 16px;
    background: linear-gradient(135deg, rgba(255,255,255,0.1) 0%, rgba(255,255,255,0.05) 100%);
    z-index: -1;
  }

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
  }

  .el-form-item {
    margin-bottom: 24px;

    &:last-child {
      margin-bottom: 0;
    }

    // 统一所有表单项的样式
    .el-form-item__content {
      width: 100%;
    }

    // 确保验证码表单项的布局正确
    &.captcha-item {
      display: flex !important;
      flex-direction: row !important;
      align-items: flex-start;

      .el-form-item__content {
        display: flex !important;
        flex-direction: row !important;
        gap: 12px;
        align-items: flex-start;
        width: 100%;
      }
    }
  }

  .el-input {
    width: 100%;

    .el-input__inner {
      height: 50px;
      border-radius: 8px;
      border: 2px solid #e8e8e8;
      background: #f8f9fa;
      color: #2c3e50;
      font-size: 16px;
      transition: all 0.3s ease;
      position: relative;
      padding-left: 45px; // 为图标留出空间

      &:focus {
        border-color: #34495e;
        background: #ffffff;
        box-shadow: 0 0 0 3px rgba(52, 73, 94, 0.1);
        transform: scale(1.02);
      }

      &::placeholder {
        color: #95a5a6;
        transition: color 0.3s ease;
      }

      &:focus::placeholder {
        color: #7f8c8d;
      }
    }
  }

  .input-icon {
    height: 50px;
    width: 16px;
    margin-left: 4px;
    color: #7f8c8d;
    position: absolute;
    left: 15px;
    top: 50%;
    transform: translateY(-50%);
    z-index: 2;
  }
}

.captcha-item {
  .captcha-container {
    display: flex !important;
    flex-direction: row !important;
    gap: 12px;
    align-items: flex-start;
    width: 100%;

    .el-input {
      flex: 1;
      min-width: 0; // 防止输入框溢出
      margin-bottom: 0 !important;

      .el-input__inner {
        padding-left: 45px; // 保持与其他输入框一致
      }
    }

    .login-code {
      flex-shrink: 0; // 防止验证码图片被压缩
      width: 120px;
      height: 50px;
      margin-left: 0 !important;
      float: none !important;

      .login-code-img {
        width: 100%;
        height: 100%;
        border-radius: 8px;
        cursor: pointer;
        border: 2px solid #e8e8e8;
        transition: all 0.3s ease;
        object-fit: cover; // 确保图片正确显示

        &:hover {
          border-color: #34495e;
          transform: scale(1.02);
          box-shadow: 0 4px 12px rgba(52, 73, 94, 0.2);
        }
      }
    }
  }
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  padding: 0 2px; // 与输入框对齐

  .remember-me {
    color: #7f8c8d;
    font-size: 14px;

    .el-checkbox__label {
      color: #7f8c8d;
    }
  }

  .register-link {
    .link-type {
      color: #34495e;
      text-decoration: none;
      font-size: 14px;
      font-weight: 500;
      transition: color 0.3s ease;

      &:hover {
        color: #2c3e50;
        text-decoration: underline;
      }
    }
  }
}

.login-button-item {
  margin-bottom: 0;
  padding: 0 2px; // 与输入框对齐

  .login-button {
    width: 100%;
    height: 50px;
    border-radius: 8px;
    background: linear-gradient(135deg, #34495e 0%, #2c3e50 100%);
    border: none;
    font-size: 16px;
    font-weight: 600;
    color: #ffffff;
    transition: all 0.3s ease;
    position: relative;
    overflow: hidden;

    &::before {
      content: '';
      position: absolute;
      top: 0;
      left: -100%;
      width: 100%;
      height: 100%;
      background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
      transition: left 0.5s;
    }

    &:hover {
      background: linear-gradient(135deg, #2c3e50 0%, #34495e 100%);
      transform: translateY(-1px);
      box-shadow: 0 8px 20px rgba(44, 62, 80, 0.3);

      &::before {
        left: 100%;
      }
    }

    &:active {
      transform: translateY(0);
    }

    &.is-loading {
      background: linear-gradient(135deg, #7f8c8d 0%, #95a5a6 100%);
    }
  }
}

.el-login-footer {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 50px;
  line-height: 50px;
  text-align: center;
  color: #bdc3c7;
  font-family: 'Arial', sans-serif;
  font-size: 12px;
  letter-spacing: 1px;
  background: rgba(44, 62, 80, 0.8);
  backdrop-filter: blur(10px);
  z-index: 3;
  animation: slideInUp 1s ease-out 0.8s both;
}

// 响应式设计
@media (max-width: 768px) {
  .login-container {
    margin: 0 15px;
  }

  .login-form {
    padding: 30px 25px;
  }

  .login-header {
    .logo {
      font-size: 28px;
    }

    .subtitle {
      font-size: 14px;
    }
  }
}

@media (max-width: 480px) {
  .login-form {
    padding: 25px 20px;
  }

  .captcha-item {
    .captcha-container {
      flex-direction: column !important;
      gap: 8px;

      .el-input {
        flex: none;
        margin-bottom: 8px !important;
      }

      .login-code {
        width: 100%;
        height: 50px;
        align-self: center;
        margin-left: 0 !important;
      }
    }
  }
}
</style>









<!--<template>-->
<!--  <div class="login">-->
<!--    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">-->
<!--      <h3 class="title">{{ title }}</h3>-->
<!--      <el-form-item prop="username">-->
<!--        <el-input-->
<!--          v-model="loginForm.username"-->
<!--          type="text"-->
<!--          auto-complete="off"-->
<!--          placeholder="账号"-->
<!--        >-->
<!--          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />-->
<!--        </el-input>-->
<!--      </el-form-item>-->
<!--      <el-form-item prop="password">-->
<!--        <el-input-->
<!--          v-model="loginForm.password"-->
<!--          type="password"-->
<!--          auto-complete="off"-->
<!--          placeholder="密码"-->
<!--          @keyup.enter.native="handleLogin"-->
<!--        >-->
<!--          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />-->
<!--        </el-input>-->
<!--      </el-form-item>-->
<!--      <el-form-item prop="code" v-if="captchaEnabled">-->
<!--        <el-input-->
<!--          v-model="loginForm.code"-->
<!--          auto-complete="off"-->
<!--          placeholder="验证码"-->
<!--          style="width: 63%"-->
<!--          @keyup.enter.native="handleLogin"-->
<!--        >-->
<!--          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />-->
<!--        </el-input>-->
<!--        <div class="login-code">-->
<!--          <img :src="codeUrl" @click="getCode" class="login-code-img"/>-->
<!--        </div>-->
<!--      </el-form-item>-->
<!--      <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>-->
<!--      <el-form-item style="width:100%;">-->
<!--        <el-button-->
<!--          :loading="loading"-->
<!--          size="medium"-->
<!--          type="primary"-->
<!--          style="width:100%;"-->
<!--          @click.native.prevent="handleLogin"-->
<!--        >-->
<!--          <span v-if="!loading">登 录</span>-->
<!--          <span v-else>登 录 中...</span>-->
<!--        </el-button>-->
<!--        <div style="float: right;" v-if="register">-->
<!--          <router-link class="link-type" :to="'/register'">立即注册</router-link>-->
<!--        </div>-->
<!--      </el-form-item>-->
<!--    </el-form>-->
<!--    &lt;!&ndash;  底部  &ndash;&gt;-->
<!--    <div class="el-login-footer">-->
<!--      <span>Copyright © 2025 YourCompany All Rights Reserved.</span>-->
<!--    </div>-->
<!--  </div>-->
<!--</template>-->

<!--<script>-->
<!--import { getCodeImg } from "@/api/login"-->
<!--import Cookies from "js-cookie"-->
<!--import { encrypt, decrypt } from '@/utils/jsencrypt'-->

<!--export default {-->
<!--  name: "Login",-->
<!--  data() {-->
<!--    return {-->
<!--      title: process.env.VUE_APP_TITLE,-->
<!--      codeUrl: "",-->
<!--      loginForm: {-->
<!--        username: "admin",-->
<!--        password: "admin123",-->
<!--        rememberMe: false,-->
<!--        code: "",-->
<!--        uuid: ""-->
<!--      },-->
<!--      loginRules: {-->
<!--        username: [-->
<!--          { required: true, trigger: "blur", message: "请输入您的账号" }-->
<!--        ],-->
<!--        password: [-->
<!--          { required: true, trigger: "blur", message: "请输入您的密码" }-->
<!--        ],-->
<!--        code: [{ required: true, trigger: "change", message: "请输入验证码" }]-->
<!--      },-->
<!--      loading: false,-->
<!--      // 验证码开关-->
<!--      captchaEnabled: true,-->
<!--      // 注册开关-->
<!--      register: false,-->
<!--      redirect: undefined-->
<!--    }-->
<!--  },-->
<!--  watch: {-->
<!--    $route: {-->
<!--      handler: function(route) {-->
<!--        this.redirect = route.query && route.query.redirect-->
<!--      },-->
<!--      immediate: true-->
<!--    }-->
<!--  },-->
<!--  created() {-->
<!--    this.getCode()-->
<!--    this.getCookie()-->
<!--  },-->
<!--  methods: {-->
<!--    getCode() {-->
<!--      getCodeImg().then(res => {-->
<!--        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled-->
<!--        if (this.captchaEnabled) {-->
<!--          this.codeUrl = "data:image/gif;base64," + res.img-->
<!--          this.loginForm.uuid = res.uuid-->
<!--        }-->
<!--      })-->
<!--    },-->
<!--    getCookie() {-->
<!--      const username = Cookies.get("username")-->
<!--      const password = Cookies.get("password")-->
<!--      const rememberMe = Cookies.get('rememberMe')-->
<!--      this.loginForm = {-->
<!--        username: username === undefined ? this.loginForm.username : username,-->
<!--        password: password === undefined ? this.loginForm.password : decrypt(password),-->
<!--        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)-->
<!--      }-->
<!--    },-->
<!--    handleLogin() {-->
<!--      this.$refs.loginForm.validate(valid => {-->
<!--        if (valid) {-->
<!--          this.loading = true-->
<!--          if (this.loginForm.rememberMe) {-->
<!--            Cookies.set("username", this.loginForm.username, { expires: 30 })-->
<!--            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 })-->
<!--            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 })-->
<!--          } else {-->
<!--            Cookies.remove("username")-->
<!--            Cookies.remove("password")-->
<!--            Cookies.remove('rememberMe')-->
<!--          }-->
<!--          this.$store.dispatch("Login", this.loginForm).then(() => {-->
<!--            this.$router.push({ path: this.redirect || "/" }).catch(()=>{})-->
<!--          }).catch(() => {-->
<!--            this.loading = false-->
<!--            if (this.captchaEnabled) {-->
<!--              this.getCode()-->
<!--            }-->
<!--          })-->
<!--        }-->
<!--      })-->
<!--    }-->
<!--  }-->
<!--}-->
<!--</script>-->

<!--<style rel="stylesheet/scss" lang="scss">-->
<!--.login {-->
<!--  display: flex;-->
<!--  justify-content: center;-->
<!--  align-items: center;-->
<!--  height: 100%;-->
<!--  background-image: url("../assets/images/9f0e84e94fc94492afda1793afaf31fb.jpg");-->
<!--  background-size: cover;-->
<!--}-->
<!--.title {-->
<!--  margin: 0px auto 30px auto;-->
<!--  text-align: center;-->
<!--  color: #707070;-->
<!--}-->

<!--.login-form {-->
<!--  border-radius: 6px;-->
<!--  background: #ffffff;-->
<!--  width: 400px;-->
<!--  padding: 25px 25px 5px 25px;-->
<!--  z-index: 1;-->
<!--  .el-input {-->
<!--    height: 38px;-->
<!--    input {-->
<!--      height: 38px;-->
<!--    }-->
<!--  }-->
<!--  .input-icon {-->
<!--    height: 39px;-->
<!--    width: 14px;-->
<!--    margin-left: 2px;-->
<!--  }-->
<!--}-->
<!--.login-tip {-->
<!--  font-size: 13px;-->
<!--  text-align: center;-->
<!--  color: #bfbfbf;-->
<!--}-->
<!--.login-code {-->
<!--  width: 33%;-->
<!--  height: 38px;-->
<!--  float: right;-->
<!--  img {-->
<!--    cursor: pointer;-->
<!--    vertical-align: middle;-->
<!--  }-->
<!--}-->
<!--.el-login-footer {-->
<!--  height: 40px;-->
<!--  line-height: 40px;-->
<!--  position: fixed;-->
<!--  bottom: 0;-->
<!--  width: 100%;-->
<!--  text-align: center;-->
<!--  color: #fff;-->
<!--  font-family: Arial;-->
<!--  font-size: 12px;-->
<!--  letter-spacing: 1px;-->
<!--  background-color: #2f4050;-->
<!--}-->
<!--.login-code-img {-->
<!--  height: 38px;-->
<!--}-->
<!--</style>-->
